﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MMS.DAO;

namespace MMS
{
    public partial class UserControlUserList : UserControl
    {
        UserDAO userDAO = new UserDAO(Param.ConnectionString.Value);
        public UserControlUserList()
        {
            InitializeComponent();
        }

        private void UserControlUserList_Load(object sender, EventArgs e)
        {
            this.Dock = DockStyle.Fill;
            gridViewUser.OptionsBehavior.AutoPopulateColumns = false;
            PopulateData();
        }

        private void PopulateData()
        {

            List<UserDTO> userList = new List<UserDTO>();
            userList = userDAO.SelectAll();
            gridControlUser.DataSource = userList;
            Id.FieldName = "Id";
            UserName.FieldName = "UserName";
            Type.FieldName = "Type";
            Email.FieldName = "Email";
            Active.FieldName = "Active";
            Address.FieldName = "Address";
            NameOfUser.FieldName = "Name";
        }

        private UserDTO getSelectedUser()
        {
            UserDTO selectedUser = null;

            string userId = gridViewUser.GetFocusedRowCellValue(Id).ToString();
            selectedUser = userDAO.Select(userId);
            return selectedUser;
        }

        private void barButtonItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string itemName = e.Item.Name;
            if (itemName.Equals(bbiAdd.Name))
            {
                using (FormUserInfo f = new FormUserInfo())
                {
                    f.Text = MessageBundle.ADD_NEW_USER.Value;
                    f.ShowDialog();
                    PopulateData();

                }
            }
            else if (itemName.Equals(bbiEdit.Name))
            {
                UserDTO selectedUser = getSelectedUser();
                using (FormUserInfo f = new FormUserInfo(selectedUser))
                {
                    f.Text = MessageBundle.EDIT_USER.Value;
                    f.ShowDialog();
                    PopulateData();
                }

            }
            else if (itemName.Equals(bbiDelete.Name))
            {
                UserDTO selectedUser = getSelectedUser();
                try
                {
                    DialogResult deleteResult = Utils.ShowAuthentication(MessageBundle.DELETE_WARNING_CONTENT.Value);
                    if (deleteResult == DialogResult.Yes)
                    {
                        userDAO.Delete(selectedUser.Id);
                        LogUtil.log.Error("You have just deleted this User" + selectedUser.Id);
                    }

                }

                catch (System.Exception ex)
                {
                    LogUtil.log.Error("can not delete this unit" + selectedUser.Id);
                    LogUtil.log.Error(ex);
                }

                PopulateData();
            }

            else if (itemName.Equals(bbiExit.Name))
            {

            }
        }


    }
}
